<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\SchoolWithdrawFundForm;
use App\Admin\Repositories\School;
use App\Admin\Table\SchoolWithdrawFundsTable;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SchoolController extends AdminController
{

    protected $status = [2 => '启用', 1 => '禁用'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new School(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('thumbnail_image')->image('', 50, 50);
            $grid->column('name');
            $grid->column('mobile');
            $grid->column('share_ratio');
            $grid->column('status')->using($this->status)->label([
                1 => Admin::color()->gray(),
                2 => Admin::color()->green(),
            ]);
            $grid->column('withdraw_fund_log', '提现记录')->display('查看')->modal(function (Grid\Displayers\Modal $modal) {
                // 传递当前行字段值
                return SchoolWithdrawFundsTable::make()->payload(['id' => $this->id]);
            });
            $grid->column('withdraw_fund', '操作')->display('提现')->modal(function (Grid\Displayers\Modal $modal) {
                return SchoolWithdrawFundForm::make()->payload(['id' => $this->id]);
            });

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id')->width(2);
                $filter->like('name')->width(4);
                $filter->equal('status')->select($this->status)->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new School(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new School(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('mobile');
            $form->decimal('share_ratio')->required();
            $form->image('thumbnail_image')->move('images/school')->uniqueName()->autoUpload()->removable(false)->required();
            $form->textarea('introduction');
            $form->radio('status')->options($this->status);
        });
    }
}
